const path = require('path');
const Koa = require('koa');
const cors = require('koa2-cors');
const serve = require('koa-static');
const bodyParser = require('koa-bodyparser')
const enforceHttps = require('koa-sslify');
const http = require('http');
const https = require('https');
const fs = require('fs');
const passport = require('./config/passport')
const config = require('./config')
const router = require('./routes');
const app = new Koa();
// const mongoose = require('mongoose')
const options = {
  key: fs.readFileSync(path.join(__dirname, './cert/cert.key')),
  cert: fs.readFileSync(path.join(__dirname, './cert/cert.pem'))

}
// mongoose.Promise = global.Promise
// mongoose.connect(config.database)

app.use(enforceHttps())

app.use(cors({
  origin: function(ctx) {
    if (ctx.url === '/test') {
      return false;
    }
    return '*';
  },
  allowMethods: ['GET', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept','Data-Type'],
}));

app.use(bodyParser())

app.use(serve(path.join(__dirname, '../dist')))

app.use(passport.initialize())
app.use(passport.session())

app
	.use(router.routes())
	.use(router.allowedMethods());

http.createServer(app.callback()).listen(3001);
https.createServer(options, app.callback()).listen(3000);